package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.AnCar;
import com.animal.entity.AnDonation;
import com.animal.entity.AnUser;
import com.animal.mapper.AnDonationMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.response.Result;
import com.animal.service.AnDonationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-08
 */
@RestController
@RequestMapping("/an-donation")
public class AnDonationController extends JSONAuthentication {
    @Autowired
    AnDonationMapper anDonationMapper;
    @Autowired
    AnDonationService anDonationService;
    @Autowired
    AnUserMapper anUserMapper;
    @ApiOperation(value = "添加捐赠数据")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody AnDonation anDonation) throws  Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anDonation.setDoTime(timestamp);
        AnUser anUser=anUserMapper.selectById(anDonation.getUserId());
        float chiyou=anUser.getBalance();
        float juanzeng=anDonation.getPrice();

        if(chiyou-juanzeng>0){
            int i=anDonationMapper.insert(anDonation);
            anUser.setBalance(chiyou-juanzeng);
            int y=anUserMapper.updateById(anUser);
            if(i>0&&y>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error().message("余额不足，捐赠失败");
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "删除捐赠记录")
    @PostMapping("/del")
    public void delCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=anDonationMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查询捐赠记录")
    @PostMapping("/look")
    public void lookAnimal(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{
        int total =anDonationMapper.selectCount(null);
        List<AnDonation> list =anDonationService.findAll(current,size);
        Result ok = Result.ok().data("donation",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }

}

